#include "stm32f10x.h"
#include "user.h"
#include "stm32f10x_bkp.h"

extern u8	g_mode;
extern u8	g_working;
extern u8	g_current_temp;
extern u8	g_goal_temp;
extern u8	g_current_hum;
extern u8	g_goal_hum;
extern s8	g_focus;
extern u8	g_current_left_time;
extern u8	g_goal_left_time;
extern u8	g_water_level;
extern u8	g_working_step;
extern u8	g_edit;

extern u32	g_begin_time;

extern u8	g_setting_flag;

extern u8	g_set_focus;

extern s16	g_temp_high;
extern s16	g_temp_low;
extern s16 g_temp_shift;
extern s16	g_hum_high;
extern s16	g_hum_low;
extern s16	g_hum_shift;

extern u8 	g_temp_alarm;
extern u8 	g_hum_alarm;

extern u8	g_test_flag;
extern u8 	g_flash_current_left_time;

extern u32 THH, TMM, TSS;
extern u32 TY, TM, TD;

extern u8	g_power_temp;
extern u8	g_power_hum;

extern void Update_Focus(u8 focus,u16 new_color);
extern void Update_Mode(u8 flag);


extern u8	g_first_time;

void led_on(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_0);
}

void led_off(void)
{
	GPIO_SetBits(GPIOC, GPIO_Pin_0);
}

void beep_off(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_4);
}

void beep_on(void)
{
	GPIO_SetBits(GPIOC, GPIO_Pin_4);
}
//alarm1 for system running or not
void alarm1_on(void)
{
	GPIO_SetBits(GPIOA, GPIO_Pin_1);
}

void alarm1_off(void)
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
//alarm2 for system temp threshold 
void alarm2_on(void)
{
	GPIO_SetBits(GPIOA, GPIO_Pin_2);
}

void alarm2_off(void)
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
//alarm3 for system hum threshold
void alarm3_on(void)
{
	GPIO_SetBits(GPIOA, GPIO_Pin_3);
}

void alarm3_off(void)
{
	GPIO_ResetBits(GPIOA, GPIO_Pin_3);
}

void beep_once(void)
{
u8 x=0;
u8 y=0;
	beep_on();
	for(x=0;x<200;x++)
	{
		for(y=0;y<200;y++)
		{}
	}
	beep_off();
}

void relay_temp_on(void)
{
unsigned char i=0;

for(i=0;i<5;i++)
{
	if(i<g_power_temp)
		GPIO_SetBits(GPIOC, GPIO_Pin_6);  //open
	else
		GPIO_ResetBits(GPIOC, GPIO_Pin_6);//close
	delay_ms(80);	
}
GPIO_ResetBits(GPIOC, GPIO_Pin_6);//close

}

void relay_temp_off(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_6);
}

void relay_hum_on(void)
{
unsigned char i=0;

	if((g_water_level==WaterOut)||(g_mode==DFO))
		GPIO_ResetBits(GPIOC, GPIO_Pin_7);
	else
	{
		for(i=0;i<5;i++)
		{
			if(i<g_power_hum)
				GPIO_SetBits(GPIOC, GPIO_Pin_7);//open	
			else
				GPIO_ResetBits(GPIOC, GPIO_Pin_7);//close
			delay_ms(80);
		}
		GPIO_ResetBits(GPIOC, GPIO_Pin_7);//close
	}	
	
}

void relay_hum_off(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_7);
}

void sd_pwr_on(void)
{
	GPIO_ResetBits(GPIOC, GPIO_Pin_8);
}

void sd_pwr_off(void)
{
 	GPIO_SetBits(GPIOC, GPIO_Pin_8);
}

void Button_Up_Process(void)
{
	if(g_test_flag==1)
		return;

	if(g_working==NotWorking)
	{
		if(g_edit==Enable)
		{
			if(g_focus==ModeChoice)
				{
				if(g_mode==YinSanTong)
					g_mode=DFO;
				else
					g_mode=YinSanTong;

				Update_Mode(g_mode);
				}
			if(g_focus==GoalTemp)
				{
				g_goal_temp++;
				if(g_goal_temp>99)
					g_goal_temp=99;
				Update_Goal_Temp(g_goal_temp);
				}
			if(g_focus==GoalHum)
				{
				g_goal_hum++;
				if(g_goal_hum>99)
					g_goal_hum=99;
				Update_Goal_Hum(g_goal_hum);
				}	
			if(g_focus==GoalTime)
				{
				g_goal_left_time++;
				g_current_left_time++;
				if(g_goal_left_time>250)
					{
					g_goal_left_time=250;
					g_current_left_time=250;
					}
					
				Update_Goal_Left_Time(g_goal_left_time);
				Update_Current_Left_Time(g_current_left_time);	
				}	
			if(g_focus==YearMark)
				{
				TY++;
				if(TY>=2100)
					TY=2100;
				}
			if(g_focus==MonthMark)
				{
				TM++;
				if(TM>12)
					TM=1;				
				}
			if(g_focus==DayMark)
				{
/*
				TD++;
				if(TD>31)
					TD=1;
*/
				TD++;
				if((TM==1)||(TM==3)||(TM==5)||(TM==7)||(TM==8)||(TM==10)||(TM==12))
					{
					if(TD>31)
					TD=1;
					}
				if((TM==4)||(TM==6)||(TM==9)||(TM==11))
					{
					if(TD>30)
					TD=1;
					}
				if(TM==2)
					{
					if(TY%4)
						{
						if(TD>28)
						TD=1;
						}
					else
						{
						if(TD>29)
						TD=1;
						}
					
					}

				}
			if(g_focus==HourMark)
				{
				THH++;
				if(THH>23)
					THH=0;
				}
			if(g_focus==MinuteMark)
				{
				TMM++;
				if(TMM>59)
					TMM=0;
				}
			if(g_focus==SecondMark)
				{
				TSS++;
				if(TSS>59)
					TSS=0;
				}

		}
		if(g_edit==Disable)
			{
			g_focus--;
			if(g_focus<ModeChoice)
				g_focus=SecondMark;
			
			Update_Focus(g_focus,FOCUS_COLOR);

			}
	}

	if(g_working==Setting)
	{
		if(g_edit==Disable)
		{
			g_set_focus--;
			if(g_set_focus<SetLowTemp)
				g_set_focus=HumPower;//SetHumAlarm;		
		}
		else
		{
			if(g_set_focus==SetLowTemp)
				{
				g_temp_low++;
				if(g_temp_low>=20)
					g_temp_low=20;
				}
			if(g_set_focus==SetHighTemp)
				{
				g_temp_high++;
				if(g_temp_high>=150)
					g_temp_high=150;				
				}
			if(g_set_focus==SetShiftTemp)
				{
				g_temp_shift++;
				if(g_temp_shift>=50)
					g_temp_shift=50;
				}
			if(g_set_focus==SetLowHum)
				{
				g_hum_low++;
				if(g_hum_low>=20)
					g_hum_low=20;
				}
			if(g_set_focus==SetHighHum)
				{
				g_hum_high++;
				if(g_hum_high>=150)
					g_hum_high=150;				
				}
			if(g_set_focus==SetShiftHum)
				{
				g_hum_shift++;
				if(g_hum_shift>=50)
					g_hum_shift=50;
				}
			if(g_set_focus==SetTempAlarm)
				{
				g_temp_alarm++;
				if(g_temp_alarm>=120)
					g_temp_alarm=120;
				}
			if(g_set_focus==SetHumAlarm)
				{
				g_hum_alarm++;
				if(g_hum_alarm>=100)
					g_hum_alarm=100;
				}
			if(g_set_focus==TempPower)
				{
				g_power_temp++;
				if(g_power_temp>=5)
					g_power_temp=5;
				}
			if(g_set_focus==HumPower)
				{
				g_power_hum++;
				if(g_power_hum>=5)
					g_power_hum=5;
				}
		}

	}

}

void Button_Enter_Process(void)
{
u16 tmp16=0;

	if(g_test_flag==1)
		return;

	tmp16=0;
	while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1)==0x00)//if enter button is still pushed)
	{
		delay_ms(10);
		tmp16++;
		if(tmp16>420)
			break;
 	}

if(tmp16<400)//less than 4 seconds
{
	if(g_working==NotWorking)
	{
		if(g_edit==Disable)
		{
			g_edit=Enable;
			Update_Focus(g_focus,FOCUS_EDIT_COLOR);		
		}
		else
		{
			g_edit=Disable;			
			Update_Focus(g_focus,FOCUS_COLOR);	
			if(g_focus>=YearMark)	
				{//save time
				/*RTC_Configuration();
				Time_Adjust();
				BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);*/
				PCF8563_Save_Time();
				}

		}

		g_setting_flag=1; //begin counting for the button pushed time, for enter setting mode.

	}

	if(g_working==Setting)
	{
		if(g_edit==Disable)
		{
			g_edit=Enable;
//			Update_Focus(g_focus,RGB565_DARKRED);		
		}
		else
		{
			g_edit=Disable;			
//			Update_Focus(g_focus,RGB565_LOWGREEN);		

		}

		g_setting_flag=1; //begin counting for the button pushed time, for exit setting mode.
	}
}

if(tmp16>=400) //more than 4 seconds
{
		if(g_working==NotWorking)
			{//entering Setting Mode
			g_working=Setting;
			FillScreen(0,319,390,479,BG_COLOR);
			Show_ADC_Seting();
			Update_Focus(g_focus,BG_COLOR);//disable focus high light
			g_set_focus=SetLowTemp;
	
			g_edit=Disable;
			g_setting_flag=0;
//			g_setting_count=0;
			g_first_time=1;
			}
		else
			{//exit Settng Mode
			g_working=NotWorking;
			g_set_focus=ModeChoice;
			FillScreen(0,319,390,479,BG_COLOR);
			Update_Focus(g_focus,RGB565_LOWGREEN);//enable focus high light
			
			FillScreen(0,319,455,479,RGB565_DARK_GRAY);//draw dark bar at the bottom

			g_edit=Disable;
			g_setting_flag=0;
//			g_setting_count=0;	
			g_first_time=1;					
			}

}


}

void Button_Down_Process(void)
{
	if(g_test_flag==1)
		return;

	if(g_working==NotWorking)
	{
		if(g_edit==Enable)
		{
			if(g_focus==ModeChoice)
				{
				if(g_mode==YinSanTong)
					g_mode=DFO;
				else
					g_mode=YinSanTong;

				Update_Mode(g_mode);
				}
			if(g_focus==GoalTemp)
				{
				g_goal_temp--;
				if(g_goal_temp<1)
					g_goal_temp=1;
				Update_Goal_Temp(g_goal_temp);
				}
			if(g_focus==GoalHum)
				{
				g_goal_hum--;
				if(g_goal_hum<1)
					g_goal_hum=1;
				Update_Goal_Hum(g_goal_hum);
				}	
			if(g_focus==GoalTime)
				{
				g_goal_left_time--;
				g_current_left_time--;
				if(g_goal_left_time<1)
					{
					g_goal_left_time=1;
					g_current_left_time=1;
					}
					
				Update_Goal_Left_Time(g_goal_left_time);
				Update_Current_Left_Time(g_current_left_time);	
				}	

			if(g_focus==YearMark)
				{
				TY--;
				if(TY<=2001)
					TY=2001;
				}
			if(g_focus==MonthMark)
				{
				TM--;
				if(TM<1)
					TM=12;				
				}
			if(g_focus==DayMark)
				{

				TD--;
				if(TD<1)
					{
					if((TM==1)||(TM==3)||(TM==5)||(TM==7)||(TM==8)||(TM==10)||(TM==12))
						TD=31;
					if((TM==4)||(TM==6)||(TM==9)||(TM==11))
						TD=30;
					if(TM==2)
						{
						if(TY%4)
							TD=28;
						else
							TD=29;
						
						}					
					}


				}
			if(g_focus==HourMark)
				{
				THH--;
				if(THH>23)
					THH=23;
				}
			if(g_focus==MinuteMark)
				{
				TMM--;
				if(TMM>59)
					TMM=59;
				}
			if(g_focus==SecondMark)
				{
				TSS--;
				if(TSS>59)
					TSS=59;
				}

		}

		if(g_edit==Disable)
			{
			g_focus++;
			if(g_focus>SecondMark)
				g_focus=ModeChoice;
			
			Update_Focus(g_focus,FOCUS_COLOR);

			}
	 }

	if(g_working==Setting)
		{
		if(g_edit==Disable)
			{
			g_set_focus++;
			if(g_set_focus>HumPower)
				g_set_focus=SetLowTemp;
			}
		else
			{
			if(g_set_focus==SetLowTemp)
				{
				g_temp_low--;
				if(g_temp_low<=-50)
					g_temp_low=-50;
				}
			if(g_set_focus==SetHighTemp)
				{
				g_temp_high--;
				if(g_temp_high<=50)
					g_temp_high=50;				
				}
			if(g_set_focus==SetShiftTemp)
				{
				g_temp_shift--;
				if(g_temp_shift<=-50)
					g_temp_shift=-50;
				}
			if(g_set_focus==SetLowHum)
				{
				g_hum_low--;
				if(g_hum_low<=-50)
					g_hum_low=-50;
				}
			if(g_set_focus==SetHighHum)
				{
				g_hum_high--;
				if(g_hum_high<=50)
					g_hum_high=50;				
				}
			if(g_set_focus==SetShiftHum)
				{
				g_hum_shift--;
				if(g_hum_shift<=-50)
					g_hum_shift=-50;
				}
			if(g_set_focus==SetTempAlarm)
				{
				g_temp_alarm--;
				if(g_temp_alarm<=80)
					g_temp_alarm=80;
				}
			if(g_set_focus==SetHumAlarm)
				{
				g_hum_alarm--;
				if(g_hum_alarm<=80)
					g_hum_alarm=80;
				}										
			if(g_set_focus==TempPower)
				{
				g_power_temp--;
				if(g_power_temp<=1)
					g_power_temp=1;
				}						
			if(g_set_focus==HumPower)
				{
				g_power_hum--;
				if(g_power_hum<=1)
					g_power_hum=1;
				}

			}	

		}

}

void Button_Run_Process(void)
{
	if(g_test_flag==1)
		return;

	if(g_working==NotWorking)
		{
		g_working=Working;
		g_working_step=PreHeating;
		Update_Working_Step(PreHeating);
		Update_Focus(g_focus,BG_COLOR);//disable focus high light
		return;
		}
	if(g_working==Working)
		{
		g_working=NotWorking;
		g_working_step=0;//none step
		g_begin_time=0;
		Update_Working_Step(0);
		g_edit=Disable;
		Update_Focus(g_focus,FOCUS_COLOR);//enable focus high light
		g_flash_current_left_time=0;
		g_current_left_time=g_goal_left_time;
		Update_Current_Left_Time(g_current_left_time);
		relay_temp_off();
		relay_hum_off();
		return;
		}
}

void Sys_Test(void)
{
u16	y=0;
u16 x=0;
y=24;
x=0;

	if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==0x01)//check if program should enter into test mode or not
	{
	return;
	}

	g_test_flag=1;

	Clr_Screen(RGB565_ORANGE);
//button1	
	Disp_Char(0,1*y,12,10,BG_COLOR,FT_COLOR);//display D on screen
	Disp_Char(12,1*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
//button2
	Disp_Char(0,2*y,12,10,BG_COLOR,FT_COLOR);//display D on screen
	Disp_Char(12,2*y,12,2,BG_COLOR,FT_COLOR);//display 2 on screen
//button3
	Disp_Char(0,3*y,12,10,BG_COLOR,FT_COLOR);//display D on screen
	Disp_Char(12,3*y,12,3,BG_COLOR,FT_COLOR);//display 3 on screen
//button4
	Disp_Char(0,4*y,12,10,BG_COLOR,FT_COLOR);//display D on screen
	Disp_Char(12,4*y,12,4,BG_COLOR,FT_COLOR);//display 4 on screen
//water shortage
	Disp_Char(0,5*y,12,15,BG_COLOR,FT_COLOR);//display - on screen
	Disp_Char(12,5*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen

//alarm 1 output
	Disp_Char(100,1*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(112,1*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen	

//alarm 2 output
	Disp_Char(100,2*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(112,2*y,12,2,BG_COLOR,FT_COLOR);//display 2 on screen	

//alarm 3 output
	Disp_Char(100,3*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(112,3*y,12,3,BG_COLOR,FT_COLOR);//display 3 on screen	

//temp output and hum output
	Disp_Char(100,4*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(112,4*y,12,12,BG_COLOR,FT_COLOR);//display D on screen
	
//show current temp 
	Disp_Char(0,6*y,12,11,BG_COLOR,FT_COLOR);//display F on screen			
	Disp_Char(12,6*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(24,6*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen

//show current hum
	Disp_Char(0,7*y,12,11,BG_COLOR,FT_COLOR);//display F on screen			
	Disp_Char(12,7*y,12,11,BG_COLOR,FT_COLOR);//display F on screen
	Disp_Char(24,7*y,12,2,BG_COLOR,FT_COLOR);//display 2 on screen

	Write_Record();	//write record to SD card for test

	beep_on();
	led_on();
	delay_ms(1000);
	beep_off();
	led_off();



	while(1)
	{	//display button1 result
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==0x00)
		{
		Disp_Char(50,1*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		Disp_Char(150,1*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		alarm1_off();
		}	
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==0x01)
		{
		Disp_Char(50,1*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		Disp_Char(150,1*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		alarm1_on();
		}	
		//display button2 result
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1)==0x00)
		{
		Disp_Char(50,2*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		Disp_Char(150,2*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		alarm2_off();
		}	
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_1)==0x01)
		{
		Disp_Char(50,2*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		Disp_Char(150,2*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		alarm2_on();
		}
		//display button3 result
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_2)==0x00)
		{
		Disp_Char(50,3*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		Disp_Char(150,3*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		alarm3_off();
		}	
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_2)==0x01)
		{
		Disp_Char(50,3*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		Disp_Char(150,3*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		alarm3_on();
		}
		//display button4 result
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3)==0x00)
		{
		Disp_Char(50,4*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		Disp_Char(150,4*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		//relay_temp_off();
		GPIO_ResetBits(GPIOC, GPIO_Pin_6);//close temp relay
		Disp_Char(170,4*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		//relay_hum_on();
		GPIO_SetBits(GPIOC, GPIO_Pin_7);  //open hum relay
		}	
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3)==0x01)
		{
		Disp_Char(50,4*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		Disp_Char(150,4*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		//relay_temp_on();
		GPIO_SetBits(GPIOC, GPIO_Pin_6);  //open temp relay
		Disp_Char(170,4*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		//relay_hum_off();
		GPIO_ResetBits(GPIOC, GPIO_Pin_7);//close hum relay
		}
				
		//display water shortage result
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==0x00)
		{
		Disp_Char(50,5*y,12,0,BG_COLOR,FT_COLOR);//display 0 on screen
		}	
		if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==0x01)
		{
		Disp_Char(50,5*y,12,1,BG_COLOR,FT_COLOR);//display 1 on screen
		}
	
	x++;
	if(x==1000)
	{
		x=0;
		g_current_temp=Get_Temp();
		Update_Temp_Test(g_current_temp);
		
		g_current_hum=Get_Hum();
		Update_Hum_Test(g_current_hum);			
		
	}
					
	}//end of while(1)



}//end of void Sys_Test(void)














